using System;
using System.Collections.Generic;
using System.Text;
using Vj.MathLibrary;
using System.Drawing;

namespace Vj.ParticlesLibrary
{
    public class FireworkEmitter : Emitter
    {
        private int particleCount = 500;
        private int flightLife = 5;

        private Color color;

        public FireworkEmitter(Vector position)
        {
            this.particleList = new List<Particle>(particleCount);

            this.color = RandomGrabber.NextColor();
            //this.color = Color.Blue;
            this.position = position;

            for (int i = 0; i < particleCount; i++)
                this.particleList.Add(this.CreateParticle());
        }

        protected override Particle CreateParticle()
        {
            // all particles will be emitted from systems center
            // so use systems position

            Vector velocity = new Vector(0.0, RandomGrabber.Next(0, 5.0), 0.0);

            velocity.Normalize();


            return new Particle(position, velocity,
                                color, 2 + RandomGrabber.Next(30));
        }

        private float explosionVelocity = 1;

        public override bool Update()
        {
            Particle particle;

            for (int i = 0; i < particleList.Count; i++)
            {
                particle = particleList[i];

                if (particle.Age >= flightLife)
                {
                    particle.Velocity = new Vector(RandomGrabber.Next(-explosionVelocity, explosionVelocity),
                             RandomGrabber.Next(-explosionVelocity, explosionVelocity),
                             RandomGrabber.Next(-explosionVelocity, explosionVelocity));
                }



                if (particle.Kill)
                {
                    //particleList.RemoveAt(i);
                    //i--;
                }
            }

            //if (particleList.Count > particleCount)
            //for (int i = particleList.Count; i < particleCount; i++)
            //particleList.Add(CreateParticle());

            return true;
        }

    }
}
